package com.wap.framework.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.wap.framework.model.Content;
import com.wap.framework.pagination.PaginationHelper;

public class ContentDAOImpl implements ContentDAO {

	private final Log log = LogFactory.getLog(ContentDAOImpl.class);

	private JdbcTemplate jdbcTemplate;

	private static String countSql = "select count(*) from dbo.dyn_page where delflag=0;";

	private static String listCurrentPage = "select * from (select id,title,status,create_time,last_time,row_number() over(order by id) as rownum from dbo.dyn_page where delflag=0) as t where t.rownum>? and t.rownum<=?";

	private static String listContent = "select id,title,status,create_time,last_time from dbo.dyn_page where delflag=0";

	private static String editContent = "select id,title,content,status,delflag from dbo.dyn_page where id=? and delflag=0";

	private static String updateContent = "update dbo.dyn_page set title=?,content=?,status=?,last_time=getdate() where id=?";

	private static String saveContent = "insert into dbo.dyn_page(owner,title,content,status,create_time,last_time,delflag) values(?,?,?,?,getdate(),getdate(),?);";

	private static String deleteContent = "update dbo.dyn_page set delflag=1 where id=?";

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Object> listContent(int pageNum) {
		PaginationHelper ph = new PaginationHelper(pageNum);
		ph.setJdbcTemplate(jdbcTemplate);
		ph.init(countSql);

		Object[] params = new Object[] { ph.getStartIndex(), ph.getEndIndex() };
		int[] types = new int[] { Types.INTEGER, Types.INTEGER };
		List<Content> contents = jdbcTemplate.query(listCurrentPage, params,
				types, new ContentRowMapper());

		List result = new ArrayList(2);
		result.add(contents);
		result.add(ph);

		return result;
	}

	@SuppressWarnings("unchecked")
	public Content editContent(int contentID) {
		Object[] params = new Object[] { contentID };
		int[] types = new int[] { Types.INTEGER };
		List<Content> content = jdbcTemplate.query(editContent, params, types,
				new EditContentRowMapper());
		if (content == null || content.size() == 0) {
			return null;
		}
		return (Content) content.get(0);
	}

	public void updateContent(Content content) {
		Object[] params = new Object[] { content.getTitle(),
				content.getContent(), content.getStatus(), content.getId() };
		int[] types = new int[] { Types.VARCHAR, Types.VARCHAR, Types.INTEGER,
				Types.INTEGER };

		int count = this.jdbcTemplate.update(updateContent, params, types);
		log.info("ContentDAOImpl#updateContent#count=" + count);
	}

	public void saveContent(Content content) {
		Object[] params = new Object[] { content.getOwner(),
				content.getTitle(), content.getContent(), content.getStatus(),
				content.getDelFlag() };
		int[] types = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
				Types.INTEGER, Types.INTEGER };

		this.jdbcTemplate.update(saveContent, params, types);
	}

	public void deleteContent(final int[] ids) {
		BatchPreparedStatementSetter bpss = new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return ids.length;
			}

			public void setValues(PreparedStatement pstmt, int i)
					throws SQLException {
				pstmt.setInt(1, ids[i]);
			}
		};
		this.jdbcTemplate.batchUpdate(deleteContent, bpss);
	}

	// RowMapper Classes
	class ContentRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int index) throws SQLException {
			Content content = new Content();
			content.setId(rs.getInt("id"));
			// content.setOwner(rs.getString("owner"));
			content.setTitle(rs.getString("title"));
			// content.setContent(rs.getString("content"));
			content.setStatus(rs.getInt("status"));
			content.setCreateTime(rs.getTimestamp("create_time"));
			content.setLastTime(rs.getTimestamp("last_time"));
			return content;
		}
	}

	class EditContentRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int index) throws SQLException {
			Content content = new Content();
			content.setId(rs.getInt("id"));
			content.setTitle(rs.getString("title"));
			content.setContent(rs.getString("content"));
			content.setStatus(rs.getInt("status"));
			content.setDelFlag(rs.getInt("delflag"));
			return content;
		}
	}
}
